/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sapra.Prefactura;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.list.DefaultListModel;
import org.sapra.dao.Periodo;
import org.sapra.dep.Practicante;
import org.sapra.dep.Usuario;
import org.sapra.dep.conexion.DBWRAP;


/**
 *
 * @author ppratsc
 */
public class wAjustes extends nextapp.echo2.app.WindowPane{
    String nombre;
    DisplayPane m_Parent;
    public wAjustes(DisplayPane parent,Periodo periodo,boolean isAdmin,String name,Practicante p, Usuario u) {
        super();
        nombre = (isAdmin)?"Deduccion: ":"Ajuste: ";
        nombre += name;
        setModal(true);
        setHeight(new Extent(400,Extent.PX));
        m_Parent = parent;
        add(new AjustesWindowContentPane(periodo,isAdmin,p,u));
    }
    private void SetWindowName() {
        setTitle(nombre);
    }

    private class AjustesWindowContentPane extends nextapp.echo2.app.ContentPane{
        //private DBWRAP pdb=null;
        private Periodo m_periodo;
        private Practicante m_practicante;
        private Usuario m_usuario;
        private boolean m_isAdmin =false;

        public AjustesWindowContentPane(Periodo periodo,boolean isAdmin,Practicante p,Usuario u) {
            super();
            m_periodo = periodo;
            m_practicante = p;
            m_usuario = u;
            m_isAdmin = isAdmin;
            initComponents();
        }


        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {

            splitPane2 = new nextapp.echo2.app.SplitPane();//GEN-BEGIN:init_components
            column3 = new nextapp.echo2.app.Column();
            radioButton6 = new nextapp.echo2.app.RadioButton();
            radioButton5 = new nextapp.echo2.app.RadioButton();
            textField12 = new nextapp.echo2.app.TextField();
            radioButton2 = new nextapp.echo2.app.RadioButton();
            radioButton1 = new nextapp.echo2.app.RadioButton();
            label11 = new nextapp.echo2.app.Label();
            textArea3 = new nextapp.echo2.app.TextArea();
            label13 = new nextapp.echo2.app.Label();
            listBox10 = new nextapp.echo2.app.ListBox();
            row4 = new nextapp.echo2.app.Row();
            button1 = new nextapp.echo2.app.Button();
            button7 = new nextapp.echo2.app.Button();
            button9 = new nextapp.echo2.app.Button();
            button2 = new nextapp.echo2.app.Button();

            add(splitPane2);

            splitPane2.setBackground(nextapp.echo2.app.Color.WHITE);

            splitPane2.setOrientation(nextapp.echo2.app.SplitPane.ORIENTATION_VERTICAL);

            splitPane2.setResizable(true);

            splitPane2.setSeparatorPosition(new nextapp.echo2.app.Extent(
                    304,
                    nextapp.echo2.app.Extent.PX));

            splitPane2.add(column3);
            splitPane2.add(row4);

            column3.setBackground(nextapp.echo2.app.Color.WHITE);

            column3.add(radioButton6);
            column3.add(radioButton5);
            column3.add(textField12);
            column3.add(radioButton2);
            column3.add(radioButton1);
            column3.add(label11);
            column3.add(textArea3);
            column3.add(label13);
            column3.add(listBox10);

            radioButton6.setForeground(new nextapp.echo2.app.Color(
                    0x011dac));

            nextapp.echo2.app.button.ButtonGroup g1 = new nextapp.echo2.app.button.ButtonGroup();
            radioButton6.setGroup(g1);

            radioButton6.setSelected(true);

            radioButton6.setText("DIAS");


            radioButton5.setForeground(new nextapp.echo2.app.Color(
                    0x011dac));

            radioButton5.setGroup(g1);

            radioButton5.setText("MONTO");


            textField12.setBackground(new nextapp.echo2.app.Color(
                    0xafafaf));

            textField12.setForeground(new nextapp.echo2.app.Color(
                    0x011dac));

            textField12.setText("1");


            nextapp.echo2.app.button.ButtonGroup g2 = new nextapp.echo2.app.button.ButtonGroup();
            radioButton2.setGroup(g2);

            radioButton2.setSelected(true);

            radioButton2.setText("DESCONTAR");


            radioButton1.setGroup(g2);

            radioButton1.setText("AGREGAR");


            label11.setFont(new nextapp.echo2.app.Font(
                    nextapp.echo2.app.Font.SANS_SERIF,
                    nextapp.echo2.app.Font.PLAIN,
                    new nextapp.echo2.app.Extent(14, nextapp.echo2.app.Extent.PX)));

            label11.setForeground(new nextapp.echo2.app.Color(
                    0x011dac));

            label11.setText("FACTOR:");


            textArea3.setBackground(new nextapp.echo2.app.Color(
                    0xafafaf));

            textArea3.setForeground(new nextapp.echo2.app.Color(
                    0x011dac));

            textArea3.setText("SIN COMENTARIO");


            label13.setForeground(new nextapp.echo2.app.Color(
                    0x011dac));

            label13.setText("Ejemplo: -1 para descontar 1 dia o 1 peso");


            listBox10.setBackground(new nextapp.echo2.app.Color(
                    0xafafaf));

            listBox10.setForeground(new nextapp.echo2.app.Color(
                    0x011dac));


            row4.add(button1);
            row4.add(button7);
            row4.add(button9);
            row4.add(button2);

            button1.setBackground(new nextapp.echo2.app.Color(
                    0xafafaf));

            button1.setStyleName("Default");

            button1.setText("LIMPIAR");

            button1.setTextAlignment(new nextapp.echo2.app.Alignment(
                    nextapp.echo2.app.Alignment.CENTER,
                    nextapp.echo2.app.Alignment.CENTER));

            button1.setTextPosition(new nextapp.echo2.app.Alignment(
                    nextapp.echo2.app.Alignment.CENTER,
                    nextapp.echo2.app.Alignment.CENTER));

            button1.addActionListener(new nextapp.echo2.app.event.ActionListener(){
                public void actionPerformed(nextapp.echo2.app.event.ActionEvent evt) {
                    limpiar(evt);
                }
            });

            button7.setBackground(new nextapp.echo2.app.Color(
                    0xafafaf));

            button7.setStyleName("Default");

            button7.setText("NUEVO");

            button7.setTextAlignment(new nextapp.echo2.app.Alignment(
                    nextapp.echo2.app.Alignment.CENTER,
                    nextapp.echo2.app.Alignment.CENTER));

            button7.setTextPosition(new nextapp.echo2.app.Alignment(
                    nextapp.echo2.app.Alignment.CENTER,
                    nextapp.echo2.app.Alignment.CENTER));

            button7.addActionListener(new nextapp.echo2.app.event.ActionListener(){
                public void actionPerformed(nextapp.echo2.app.event.ActionEvent evt) {
                    grabar(evt);
                }
            });

            button9.setBackground(new nextapp.echo2.app.Color(
                    0xafafaf));

            button9.setStyleName("Default");

            button9.setText("ELIMINAR");

            button9.setTextAlignment(new nextapp.echo2.app.Alignment(
                    nextapp.echo2.app.Alignment.CENTER,
                    nextapp.echo2.app.Alignment.CENTER));

            button9.setTextPosition(new nextapp.echo2.app.Alignment(
                    nextapp.echo2.app.Alignment.CENTER,
                    nextapp.echo2.app.Alignment.CENTER));

            button9.addActionListener(new nextapp.echo2.app.event.ActionListener(){
                public void actionPerformed(nextapp.echo2.app.event.ActionEvent evt) {
                    eliminar(evt);
                }
            });

            button2.setBackground(new nextapp.echo2.app.Color(
                    0xafafaf));

            button2.setStyleName("Default");

            button2.setText("SALIR");

            button2.setTextAlignment(new nextapp.echo2.app.Alignment(
                    nextapp.echo2.app.Alignment.CENTER,
                    nextapp.echo2.app.Alignment.CENTER));

            button2.setTextPosition(new nextapp.echo2.app.Alignment(
                    nextapp.echo2.app.Alignment.CENTER,
                    nextapp.echo2.app.Alignment.CENTER));

            button2.addActionListener(new nextapp.echo2.app.event.ActionListener(){
                public void actionPerformed(nextapp.echo2.app.event.ActionEvent evt) {
                    salir(evt);
                }
            });//GEN-END:init_components

            //Pone el Nombre del Practicante a la ventana
            SetWindowName();

            loadInitialData();

        }// </editor-fold>
        class Ajuste {
            public int ajusteid=0;
            public double factor=0;
            public String comentario = null;
            public boolean tiempo =false;
            public int periodo=0;
            public boolean descuento=true;
            public boolean interno=false;


            @Override
            public String toString(){
                String resp = (interno)?"Deduccion por":"Ajuste por ";
                resp += (tiempo)?"Tiempo de ":"Monto de ";
                if(factor < 0)
                    resp += "menos " + (factor*-1);
                else
                    resp += "mas " + factor;

                resp += (tiempo)?" Dias ":" Pesos";
                resp += " / " + comentario;
                return resp;
            }
        }

        private void loadInitialData() {
            DBWRAP db=new DBWRAP();
            DefaultListModel lm = new DefaultListModel();
            ResultSet rs;
            Ajuste a;
            try {
                String sql = "select ajusteid,factor,interno,cometario,tiempo from facturaajuste where fk_periodoid = " + m_periodo.index;
                sql += " and fk_convenioid = " + m_practicante.id;
                sql += " and interno = ";
                sql += (m_isAdmin)?"TRUE":"FALSE";
                
                rs = db.select(sql);
                while(rs.next()){
                    a = new Ajuste();
                    a.ajusteid = rs.getInt("ajusteid");
                    a.factor = rs.getDouble("factor");
                    a.comentario = rs.getString("cometario");
                    a.tiempo = rs.getBoolean("tiempo");
                    a.interno = rs.getBoolean("interno");
                    a.periodo = m_periodo.index;
                    lm.add(a);

                }
                if(db!=null){
                    db.close();
                    db=null;
                }
                if(rs!=null){
                    rs.close();
                    rs=null;
                }
                

            } catch (SQLException ex) {
                Logger.getLogger(wAjustes.class.getName()).log(Level.SEVERE, null, ex);
               if(db!=null){
                    db.close();
                    db=null;
                }

            }
            finally{
                if(db!=null){
                    db.close();
                    db=null;
                }
            }

            // Populate del ListBox
            listBox10.setModel(lm);

            return;
        }

        // Action listeners//GEN-LINE:listeners_area

        private void limpiar(nextapp.echo2.app.event.ActionEvent evt) {//GEN-FIRST:limpiar_nextapp.echo2.app.event.ActionEvent
            textField12.setText("");
            textArea3.setText("");
            radioButton6.setSelected(true);
        }//GEN-LAST:limpiar_nextapp.echo2.app.event.ActionEvent

        private void salir(nextapp.echo2.app.event.ActionEvent evt) {//GEN-FIRST:salir_nextapp.echo2.app.event.ActionEvent
            wAjustes win = (wAjustes)getParent();
            m_Parent.UpdateView(false);
            win.userClose();
        }//GEN-LAST:salir_nextapp.echo2.app.event.ActionEvent

        private void eliminar(nextapp.echo2.app.event.ActionEvent evt) {//GEN-FIRST:eliminar_nextapp.echo2.app.event.ActionEvent
            String sql = "delete from facturaajuste where ajusteid=";
           Ajuste a;
           DBWRAP db=new DBWRAP();
            try {
                a = (Ajuste) listBox10.getSelectedValue();
            }catch(Exception ex){
                return;
            }

            if(a == null)return;

            sql += a.ajusteid;
            try {
                db.update(sql);
                 if(db!=null){
                    db.close();
                    db=null;
                }
            } catch (SQLException ex) {
                 if(db!=null){
                    db.close();
                    db=null;
                }
                Logger.getLogger(wAjustes.class.getName()).log(Level.SEVERE, null, ex);
            }finally{
                 if(db!=null){
                    db.close();
                    db=null;
                }
            }


                loadInitialData();
        }//GEN-LAST:eliminar_nextapp.echo2.app.event.ActionEvent

        private void grabar(nextapp.echo2.app.event.ActionEvent evt) {//GEN-FIRST:grabar_nextapp.echo2.app.event.ActionEvent
            String sql = "insert into facturaajuste(factor,cometario,tiempo,fk_convenioid,interno,fk_periodoid,fk_usuarioId,fecha) values(";
            double factor = Double.parseDouble(textField12.getText());
            DBWRAP db=new DBWRAP();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            sql +=(radioButton1.isSelected())?factor:factor*-1;
            sql += ",'"+textArea3.getText()+"',";
            sql += (this.radioButton6.isSelected())?"TRUE,":"FALSE,";
            sql += m_practicante.id + ",";
            sql += (m_isAdmin)?"TRUE,":"FALSE,";
            sql += m_periodo.index + ",";
            sql += m_usuario.id+",";
            sql += "now())";

            try {
                db.update(sql);
                 if(db!=null){
                    db.close();
                    db=null;
                }
            } catch (SQLException ex) {
                 if(db!=null){
                    db.close();
                    db=null;
                }
                Logger.getLogger(wAjustes.class.getName()).log(Level.SEVERE, null, ex);
            }finally{
                 if(db!=null){
                    db.close();
                    db=null;
                }
            }


            loadInitialData();


        }//GEN-LAST:grabar_nextapp.echo2.app.event.ActionEvent



        // Variables declaration - do not modify//GEN-BEGIN:variables_declaration
        private nextapp.echo2.app.SplitPane splitPane2;
        private nextapp.echo2.app.Column column3;
        private nextapp.echo2.app.RadioButton radioButton6;
        private nextapp.echo2.app.RadioButton radioButton5;
        private nextapp.echo2.app.TextField textField12;
        private nextapp.echo2.app.RadioButton radioButton2;
        private nextapp.echo2.app.RadioButton radioButton1;
        private nextapp.echo2.app.Label label11;
        private nextapp.echo2.app.TextArea textArea3;
        private nextapp.echo2.app.Label label13;
        private nextapp.echo2.app.ListBox listBox10;
        private nextapp.echo2.app.Row row4;
        private nextapp.echo2.app.Button button1;
        private nextapp.echo2.app.Button button7;
        private nextapp.echo2.app.Button button9;
        private nextapp.echo2.app.Button button2;//GEN-END:variables_declaration

    }
}
